package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsDao;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.OrdbyModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsDaoImpl implements INewsDao {
    JDBC jdbc=new JDBC();
    @Override
    public List<NewsModel> findAll() {
        String sql = "select * FROM news ORDER BY date desc";
        ResultSet resultSet = jdbc.getResultSet(sql);
        List<NewsModel> list=new ArrayList<>();
        try {
            while (resultSet.next()){
                NewsModel newsModel=new NewsModel();
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setDate(resultSet.getString("date"));
                newsModel.setAuthor(resultSet.getString("author"));
                newsModel.setContent(resultSet.getString("content"));
                list.add(newsModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        jdbc.closeConn();
        return list;
    }

    @Override
    public int deleteById(int newsId) {
        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.dao.INewsDao.deleteById",newsId);

    }

    @Override
    public NewsModel findById(int newsid) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.findById",newsid);

    }

    @Override
    public int newsUpdate(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsDao.newsUpdate",newsModel);
    }

    @Override
    public int newsAdd(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.INewsDao.newsAdd",newsModel);
    }


    @Override
    public int getCount(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.getCount",newsModel);
    }

    @Override
    public List<NewsModel> newsFindAllsom() {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.newsFindAllsom");
    }

    @Override
    public List<NewsModel> NewsTypeFindAll() {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.NewsTypeFindAll");
    }

    @Override
    public List<NewsModel> newsNimitFind(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.newsNimitFind",newsModel);
    }

    @Override
    public List<NewsModel> newstypeFind(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.newstypeFind",newsModel);
    }

    @Override
    public int updateNumber(int newsid) {
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsDao.updateNumber",newsid);
    }

    @Override
    public int getTypeCont(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsDao.getTypeCont",newsModel);
    }


    @Override
    public List<NewsModel> newsFindAllTh(NewsModel newsModel) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsDao.newsFindAllTh",newsModel);
    }
}
